package com.hellogood.yaohuo.client.basic;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.client.common.CommonMessages;
import com.hellogood.yaohuo.shared.entity.Color;
import com.hellogood.yaohuo.shared.services.ColorService;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;

public class ColorController {

	private TreeNode[] treeNodes = null;

	private HLayout layout = null;
	private TreeGrid treeGrid = null;
	private Button addBtn = null;
	private Button delBtn = null;
	private Button addTypeBtn = null;
	private Button delTypeBtn = null;

	public ColorController() {
		initData();
		initView();
	}

	public void initData() {
		ColorService.Util.getInstance().getAllColorList(new GetAllAsyncCallback());
	}

	private void initView() {
		TreeGridField codeField = new TreeGridField("code");
		codeField.setHidden(true);
		TreeGridField nameField = new TreeGridField("name", "颜色");
		TreeGridField superCodeField = new TreeGridField("superCode");
		superCodeField.setHidden(true);
		treeGrid = new TreeGrid();
		treeGrid.setFields(codeField, nameField, superCodeField);
		treeGrid.setWidth("70%");
		treeGrid.addSelectionChangedHandler(new TreeSelectionHandler());

		addTypeBtn = new Button("增加色系");
		addTypeBtn.setSize("100", "30");
		// addBtn.setMargin(10);
		addTypeBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				ColorModifyWindow window = new ColorModifyWindow(ColorController.this, "type", "0", null);
				window.setSize("300", "200");
				window.centerInPage();
				window.show();
				window.getTextItem().focusInItem();
			}
		});

		delTypeBtn = new Button("删除色系");
		delTypeBtn.setDisabled(true);
		delTypeBtn.setSize("100", "30");

		addBtn = new Button("增加颜色");
		addBtn.setSize("100", "30");
		addBtn.setDisabled(true);
		addBtn.addClickHandler(new AddBtnClickHandler());

		delBtn = new Button("删除颜色");
		delBtn.setDisabled(true);
		delBtn.setSize("100", "30");
		// delBtn.setMargin(10);
		delBtn.addClickHandler(new DelBtnClickHandler());

		VLayout btnLayout = new VLayout();
		btnLayout.setMembers(addTypeBtn, delTypeBtn, addBtn, delBtn);
		btnLayout.setMembersMargin(10);
		btnLayout.setHeight(500);
		btnLayout.setWidth("30%");

		layout = new HLayout();
		layout.addMember(treeGrid);
		layout.addMember(btnLayout);
		layout.setWidth100();
		layout.setHeight100();
	}

	public Canvas getCanvas() {
		return layout;
	}

	private class GetAllAsyncCallback implements AsyncCallback<List<Color>> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(List<Color> result) {
			if (result != null) {
				treeNodes = new TreeNode[result.size()];
				for (int i = 0; i < result.size(); i++) {
					Color c = result.get(i);
					TreeNode r = new TreeNode();
					r.setAttribute("nodeId", c.getColorCode());
					r.setAttribute("code", c.getColorCode());
					r.setAttribute("name", c.getColorCode() + "-" + c.getColorName());
					r.setAttribute("superCode", c.getSuperCode());
					r.setAttribute("isOpen", true);
					if (c.getSuperCode() != null && c.getSuperCode().equals("0")) {
						r.setIsFolder(true);
					}
					r.setAttribute("parentId", c.getSuperCode());
					treeNodes[i] = r;
				}

				Tree tree = new Tree();
				tree.setModelType(TreeModelType.PARENT);
				tree.setNameProperty("name");
				tree.setRootValue("0");
				tree.setIdField("nodeId");
				tree.setParentIdField("parentId");
				tree.setOpenProperty("isOpen");
				tree.setData(treeNodes);

				treeGrid.setData(tree);
			}
		}
	}

	private class TreeSelectionHandler implements SelectionChangedHandler {

		@Override
		public void onSelectionChanged(SelectionEvent event) {
			ListGridRecord r = treeGrid.getSelectedRecord();
			if (r != null) {
				if (r.getAttribute("superCode") != null && r.getAttribute("superCode").equals("0")) {
					addBtn.setDisabled(false);
					delTypeBtn.setDisabled(false);
					delBtn.setDisabled(true);
				} else {
					addBtn.setDisabled(true);
					delBtn.setDisabled(false);
					delTypeBtn.setDisabled(true);
				}
			} else {
				addBtn.setDisabled(true);
				delBtn.setDisabled(false);
			}
		}
	}

	private class AddBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			Record r = treeGrid.getSelectedRecord();
			if (r != null) {
				ColorModifyWindow window = new ColorModifyWindow(ColorController.this, "color", r.getAttribute("code"), r.getAttribute("name"));
				window.setSize("300", "200");
				window.centerInPage();				
				window.show();
				window.getTextItem().focusInItem();
			}
		}

	}

	private class DelBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			ListGridRecord r = treeGrid.getSelectedRecord();
			if (r != null) {
				SC.confirm(CommonMessages.ConfirmDelete, new DeleteBooleanCallback(r));
			} else {
				delBtn.setDisabled(true);
			}
		}

	}

	private class DeleteBooleanCallback implements BooleanCallback {
		private ListGridRecord r;

		public DeleteBooleanCallback(ListGridRecord record) {
			this.r = record;
		}

		@Override
		public void execute(Boolean value) {

			if (value) {
				Color c = new Color();
				c.setColorCode(r.getAttribute("code"));
				c.setColorName(r.getAttribute("name"));
				ColorService.Util.getInstance().deleteColor(c, new AsyncCallback<String>() {

					@Override
					public void onFailure(Throwable caught) {
						SC.say(caught.getMessage());

					}

					@Override
					public void onSuccess(String result) {
						if (result.equals("")) {
							SC.say(CommonMessages.DeleteColorSuccess);
							ColorController.this.initData();
						} else {
							SC.say(result);
						}
					}
				});
			}
		}
	}
}
